﻿using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Management;

namespace Lottery.App.Utility
{
    public class CryptHelper
    {
        private static readonly byte[] IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
        private static readonly byte[] Key = new byte[] { 0x38, 0x34, 0x38, 0x32, 0x35, 0x32, 0x36, 0x33 };

        public static string DeCryptDES(string decryptString)
        {
            try
            {
                byte[] buffer = Convert.FromBase64String(decryptString);
                using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, provider.CreateDecryptor(Key, IV), CryptoStreamMode.Write))
                        {
                            cs.Write(buffer, 0, buffer.Length);
                            cs.FlushFinalBlock();
                        }

                        return Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
            }
            catch {
                return "error";
            }
        }

        public static string GetCpuSerialNumber()
        {
            try
            {
                ManagementObjectCollection moc = new ManagementClass("Win32_Processor").GetInstances();

                foreach (ManagementObject mo in moc)
                {
                    return mo.Properties["ProcessorId"].Value.ToString();
                }
            }
            catch { }

            return "CpuSerialNumber";
        }

    }
}
